// utils/util.js 中需包含 jsonToString、jsonToBool 方法
import { Util } from '../../utils/util';
import { Engine } from '../engine';
import { Store } from '../store/store';

export class WareHouseFilter {
    constructor(storeId,likeName) {
        this.name = null;
        this.likeName = likeName;
        this.contactPerson = null;
        this.enable = null;
        this.lock = null;
        this.storeId = storeId || Engine.profile?.filterStore?.id || null;
    }

    static empty() {
        return new WareHouseFilter();
    }

    clear() {
        this.name = null;
         this.likeName = null;
        this.contactPerson = null;
        this.enable = null;
        this.lock = null;
    }

    static clone(filter) {
        const cloned = new WareHouseFilter();
        cloned.name = filter.name;
        cloned.likeName = filter.likeName;
        cloned.contactPerson = filter.contactPerson;
        cloned.enable = filter.enable;
        cloned.lock = filter.lock;
        cloned.storeId = filter.storeId;
        return cloned;
    }

    toJson() {
        const ret = {};
        if (this.storeId) {
            ret.storeId = this.storeId;
        }
        if (this.name) {
            ret.storageName = this.name;
        }
        if (this.likeName) {
            ret.lke_storageName = this.likeName;
        }
        return ret;
    }

    toCopyJson() {
        const ret = {
            id: this.id,
            name: this.name,
            storeId: this.storeId,
            enable: this.enable ? 1 : 0,
            lock: this.lock ? 1 : 0,
        };
        return ret;
    }

    static fromJson(json) {
        let filter = new WareHouseFilter();
        filter.id = Util.jsonToString(json.id);
        filter.name = Util.jsonToString(json.name);
        filter.storeId = Util.jsonToString(json.storeId);
        filter.enable = Util.jsonToBool(json.enable, true);
        filter.lock = Util.jsonToBool(json.lock, false);
    }

}

export class WareHouse {
    constructor({
        id,
        name,
        store = null,
        contactPerson = '',
        contactPhoneNum = '',
        addr = '',
        enabled = true,
        locked = false,
        remark = ''
    }) {
        this.id = id;
        this.name = name;
        this.contactPerson = contactPerson;
        this.contactPhoneNum = contactPhoneNum;
        this.addr = addr;
        this.enabled = enabled;
        this.locked = locked;
        this.remark = remark;
        this.store = store || Engine.profile?.getValideStore || null;
    }

    static empty() {
        return new WareHouse({
            id: '',
            name: '',
            contactPerson: '',
            contactPhoneNum: '',
            addr: '',
            enabled: true,
            locked: false,
            remark: '',
            store: Engine.profile?.getValideStore || null
        });
    }

    static clone(house) {
        return new WareHouse({
            id: house.id,
            name: house.name,
            contactPerson: house.contactPerson,
            contactPhoneNum: house.contactPhoneNum,
            addr: house.addr,
            enabled: house.enabled,
            locked: house.locked,
            remark: house.remark,
            store: house.store
        });
    }

    static fromJson(json) {
        return new WareHouse({
            id: Util.jsonToString(json.id),
            name: Util.jsonToString(json.storageName),
            contactPerson: Util.jsonToString(json.contactUser),
            contactPhoneNum: Util.jsonToString(json.contactPhone),
            addr: Util.jsonToString(json.storageAddr),
            remark: Util.jsonToString(json.remark),
            enabled: Util.jsonToBool(json.enabled, true),
            locked: json.locked || false,
            store: json.storeId ? Store.fromJsonForRelatedSource(json) : null
        });
    }

    toJson() {
        const ret = {};
        ret.orgId = Engine.profile?.org?.orgId;
        if (this.id) {
            ret.id = this.id;
        }
        ret.storageName = this.name;
        ret.contactUser = this.contactPerson;
        ret.contactPhone = this.contactPhoneNum;
        ret.storageAddr = this.addr;
        ret.remark = this.remark;
        ret.enabled = this.enabled ? 0 : 1; // Dart 为 true 表启用，JS 中 0 表启用
        ret.locked = this.locked ? 0 : 1;
        if (this.store) {
            ret.storeId = this.store.id;
        }
        return ret;
    }

    // 对象传递拷贝 与  fromJson 一一对应
    toCopyJson() {
        return {
            id: this.id,
            storageName: this.name,
            contactUser: this.contactPerson,
            contactPhone: this.contactPhoneNum,
            storageAddr: this.addr,
            remark: this.remark,
            enabled: this.enabled,
            locked: this.locked,
            ...this.store?.toCopyJsonForRelatedSource()
        };
    }
}
